<%= render partial: 'spree/admin/shared/order_tabs', locals: { current: :customer_returns } %>

<% content_for :page_title do %>
  / <%= Spree.t(:reimbursement) %> #<%= @reimbursement.number %>
<% end %>

<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @reimbursement } %>

<%= form_for [:admin, @order, @reimbursement] do |f| %>
  <fieldset class='no-border-bottom'>
    <legend><%= Spree.t(:items_to_be_reimbursed) %></legend>
    <div class="table-responsive">
      <table class="table table-condensed table-bordered reimbursement-return-items-table">
        <thead>
          <tr>
            <th><%= Spree.t(:product) %></th>
            <th><%= Spree.t(:preferred_reimbursement_type) %></th>
            <th><%= Spree.t(:reimbursement_type_override) %></th>
            <th><%= Spree.t(:pre_tax_refund_amount) %></th>
            <th><%= Spree.t(:total) %></th>
            <th><%= Spree.t(:exchange_for) %></th>
          </tr>
        </thead>
        <tbody>
          <%= f.fields_for :return_items, @reimbursement.return_items.sort_by(&:id) do |item_fields| %>
            <% return_item = item_fields.object %>
            <tr>
              <td>
                <div class="variant-name">
                  <%= link_to return_item.inventory_unit.variant.name, spree.edit_admin_product_path(return_item.inventory_unit.variant.product) %>
                </div>
                <div class="variant-options"><%= return_item.inventory_unit.variant.options_text %></div>
              </td>
              <td>
                <%= reimbursement_type_name(return_item.preferred_reimbursement_type) %>
              </td>
              <td>
                <%= item_fields.select(:override_reimbursement_type_id,
                  reimbursement_types.collect { |r| [r.name.humanize, r.id] },
                  {include_blank: true},
                  {class: 'select2'}
                ) %>
              </td>
              <td>
                <%= item_fields.text_field :pre_tax_amount, { class: 'refund-amount-input form-control' } %>
              </td>
              <td>
                <%= return_item.display_total %>
              </td>
              <td>
                <% if return_item.exchange_processed? %>
                  <%= return_item.exchange_variant.exchange_name %>
                <% else %>
                  <%= item_fields.collection_select :exchange_variant_id, return_item.eligible_exchange_variants, :id, :exchange_name, { include_blank: true }, { class: "select2-clear return-item-exchange-selection" } %>
                <% end %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    </div>
  </fieldset>

  <div class="form-actions filter-actions actions" data-hook="buttons">
    <%= button Spree.t('actions.update'), 'update' %>
  </div>
<% end %>

<fieldset class="mt-3">
  <legend><%= Spree.t(:calculated_reimbursements) %></legend>
  <div class="table-responsive">
    <table class="table table-condensed table-bordered">
      <thead data-hook="customer_return_header">
        <tr>
          <th><%= Spree.t(:reimbursement_type) %></th>
          <th><%= Spree.t(:description) %></th>
          <th><%= Spree.t(:amount) %></th>
        </tr>
      </thead>
      <tbody>
        <% @reimbursement_objects.each do |reimbursement_object| %>
          <tr data-hook="reimbursement_reimbursement_object_row">
            <td><%= reimbursement_object.class.name.demodulize %></td>
            <td><%= reimbursement_object.description %></td>
            <td><%= reimbursement_object.display_amount %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
  <% if @order.has_non_reimbursement_related_refunds? %>
    <div class="alert alert-danger">
      <%= "#{Spree.t('note')}: #{Spree.t('this_order_has_already_received_a_refund')}. #{Spree.t('make_sure_the_above_reimbursement_amount_is_correct')}." %>
    </div>
  <% end %>
  <div class="form-actions" data-hook="reimburse-buttons">
    <% if !@reimbursement.reimbursed? %>
      <%= button_to [:perform, :admin, @order, @reimbursement], { class: 'btn btn-primary', method: 'post' } do %>
        <span class="icon icon-save"></span>
        <%= Spree.t(:reimburse) %>
      <% end %>
      <span class="or"><%= Spree.t(:or) %></span>
      <%= button_link_to Spree.t('actions.cancel'), url_for([:edit, :admin, @order, @reimbursement.customer_return]), icon: 'cancel' %>
    <% end %>
  </div>
</fieldset>
